package com.dyzx.hbb.modules.attendance.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.base.RestTimeResult;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.common.utils.Utils;
import com.dyzx.hbb.modules.attendance.entity.AttendanceLeaveApplication;
import com.dyzx.hbb.modules.attendance.query.AttendanceLeaveApplicationQuery;
import com.dyzx.hbb.modules.attendance.service.AttendanceLeaveApplicationService;
import com.dyzx.hbb.modules.attendance.vo.AttendanceLeaveApplicationApprovalUVO;
import com.dyzx.hbb.modules.attendance.vo.AttendanceLeaveApplicationUpdateUVO;
import com.dyzx.hbb.modules.attendance.vo.AttendanceLeaveApplicationVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 请假申请接口
 */
@RequestMapping("/attendance/leave/application")
@RestController
public class AttendanceLeaveApplicationController extends BaseValidController<AttendanceLeaveApplicationService, AttendanceLeaveApplication, AttendanceLeaveApplicationQuery, AttendanceLeaveApplicationVO, AttendanceLeaveApplicationUpdateUVO> {
    @Autowired
    private AttendanceLeaveApplicationService service;

    @Override

    protected AttendanceLeaveApplication addValid(AttendanceLeaveApplicationVO entity) {
        Long userId = SecurityContextHolder.getUserId();
        AttendanceLeaveApplication data = new AttendanceLeaveApplication();
        BeanUtil.copyProperties(entity, data);
        String s = Utils.generateYyMMOrderNumber("AL");
        data.setNo(s);
        data.setUserId(userId);
        RestTimeResult restTimeResult = Utils.calculateRestTime(data.getStartTime(), data.getEndTime(), 8);
        data.setLeaveDays(restTimeResult.getRestDays());
        data.setLeaveHours(restTimeResult.getRestHours());
        //  data.setTotalRestHours(restTimeResult.getTotalRestHours());
        return data;
    }

    @Override
    protected AttendanceLeaveApplication editValid(AttendanceLeaveApplicationUpdateUVO entity) {
        return null;
    }

    @Override
    protected AttendanceLeaveApplicationService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<AttendanceLeaveApplication> queryWrapper(AttendanceLeaveApplicationQuery query) {
        MyLambdaQueryWrapper<AttendanceLeaveApplication> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(AttendanceLeaveApplication::getId, AttendanceLeaveApplication::getStatus, AttendanceLeaveApplication::getNo, AttendanceLeaveApplication::getUserId, AttendanceLeaveApplication::getCategoryId, AttendanceLeaveApplication::getType, AttendanceLeaveApplication::getName, AttendanceLeaveApplication::getStartTime, AttendanceLeaveApplication::getEndTime, AttendanceLeaveApplication::getLeaveDays, AttendanceLeaveApplication::getLeaveDays, AttendanceLeaveApplication::getApprovalUserId, AttendanceLeaveApplication::getApprovalTime, AttendanceLeaveApplication::getApprovalComment, AttendanceLeaveApplication::getReturnUserId, AttendanceLeaveApplication::getReturnTime, AttendanceLeaveApplication::getReturnComment);
       //category name
        queryWrapper.select("(SELECT name FROM attendance_leave_category WHERE id = attendance_leave_application.category_id LIMIT 1)").as("category_name");
        queryWrapper.userName(AttendanceLeaveApplication::getUserId).as("user_name");
        queryWrapper.userName(AttendanceLeaveApplication::getApprovalUserId).as("approval_user_name");
        queryWrapper.userName(AttendanceLeaveApplication::getReturnUserId).as("return_user_name");


        if (query != null) {
            //  queryWrapper.eq(AttendanceLeaveApplication::getUserId,query.getUserId());
            queryWrapper.eq(query.getStatus() != null, AttendanceLeaveApplication::getStatus, query.getStatus());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<AttendanceLeaveApplication> queryInfoWrapper() {
        MyLambdaQueryWrapper<AttendanceLeaveApplication> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(AttendanceLeaveApplication::getId, AttendanceLeaveApplication::getStatus, AttendanceLeaveApplication::getNo, AttendanceLeaveApplication::getUserId, AttendanceLeaveApplication::getCategoryId, AttendanceLeaveApplication::getType, AttendanceLeaveApplication::getName, AttendanceLeaveApplication::getStartTime, AttendanceLeaveApplication::getEndTime, AttendanceLeaveApplication::getLeaveDays, AttendanceLeaveApplication::getLeaveDays, AttendanceLeaveApplication::getApprovalUserId, AttendanceLeaveApplication::getApprovalTime, AttendanceLeaveApplication::getApprovalComment, AttendanceLeaveApplication::getReturnUserId, AttendanceLeaveApplication::getReturnTime, AttendanceLeaveApplication::getReturnComment);

        return queryWrapper;
    }

    //审核请假申请
    @RequestMapping("/approval")
    public Result<Boolean> approval(@RequestBody AttendanceLeaveApplicationApprovalUVO entity) {
        /**
         * 申请状态：默认(1)1-待审批，2-已通过，3-已拒绝，4-已撤销
         */
        Long userId = SecurityContextHolder.getUserId();
        Integer status = entity.getStatus();
        if (status > 0) {
            Long id = entity.getId();
            AttendanceLeaveApplication data = service.getById(id);
            if (data == null) {
                return Result.error("请假申请不存在");
            }

            if (status == 2) {
                data.setApprovalComment(entity.getApprovalComment());
                data.setApprovalUserId(userId);
                data.setApprovalTime(System.currentTimeMillis());
                data.setStatus(2);

            } else if (status == 3) {
                data.setReturnComment(entity.getReturnComment());
                data.setReturnUserId(userId);
                data.setReturnTime(System.currentTimeMillis());
                data.setStatus(3);
            } else if (status == 4) {
                data.setReturnComment(entity.getReturnComment());
                data.setReturnUserId(userId);
                data.setReturnTime(System.currentTimeMillis());
                data.setStatus(4);
            }
            boolean update = service.updateById(data);
            if (update) {
                return Result.ok();
            } else {
                return Result.error("更新失败");
            }
        } else {
            return Result.error("状态错误");
        }

    }
}
